package cn.com.yang.datastruct.queue;

/**
 * 优先级队列
 * @since 2023.07.29
 */
public class PriorityQueue {
    private int[] elements;
    private int maxSize;

    private int nElms;

    public PriorityQueue(int size){
        elements = new int[size];
        maxSize =size;
        nElms=0;
    }
    //插入数据
    public void insert(int data){
        if (nElms==0){
            elements[nElms++] = data;
        }else {
            int j= nElms -1;
            while (j>0 && data>elements[j]){
                elements[j+1]=elements[j];
                j--;
            }
            elements[j+1] = data;
            nElms++;
        }
    }
    public int peek(){
        if (nElms!=0){
            return elements[nElms-1];
        }else{
            return -1;//表示没有数据
        }
    }
    public int remove(){
        int removeData = -1;
        if (nElms !=0){
            removeData = elements[nElms-1];
            elements[nElms-1]=-1;//表示删除
        }else {
            return -1;
        }
        return removeData;
    }

    public boolean isEmpty(){
        return nElms ==0;
    }

    public static void main(String[] args) {
        PriorityQueue queue = new PriorityQueue(100);
        queue.insert(1);
        queue.insert(100);
        queue.insert(23);
        while (!queue.isEmpty()){
            System.out.println(queue.remove());
        }
    }
}
